import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { Table, Button } from 'antd'
import Util from 'utils'

export default class CustomTable extends PureComponent {
  constructor (props) {
    super(props)
    this.state = {}
    this.onSelectChange = this.onSelectChange.bind(this)
  }

  // 多选
  onSelectChange (selectedRowKeys, selectedRows) {
    this.props.multipleSelect(selectedRowKeys, selectedRows)
  }

  render () {
    const {
      addNew,
      loading,
      dataSource,
      columns,
      pagination,
      onChange,
      addNewText,
      restProps,
      size,
      multiple,
      bordered,
      selectedRowKeys,
      scroll
    } = this.props

    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange,
      getCheckboxProps: record => ({
        disabled: record.disabled
      })
    }
    const addNewType = typeof addNew
    const addNewTextStr = addNewText || '新建'
    return (
      <div>
        <div style={{ marginBottom: '15px' }}>
          {addNewType === 'function' && (
            <Button type='primary' onClick={addNew}>
              {/* <Icon type="plus" /> */}
              {addNewTextStr}
            </Button>
          )}
          {addNewType === 'string' && (
            <Button type='primary' onClick={() => Util.router.push(addNew)}>
              {/* <Icon type="plus" /> */}
              {addNewTextStr}
            </Button>
          )}
        </div>
        <Table
          rowSelection={multiple ? rowSelection : null}
          rowKey={(record, index) => index}
          loading={loading}
          dataSource={dataSource}
          onChange={onChange}
          columns={columns}
          pagination={pagination}
          bordered={bordered}
          scroll={scroll}
          size={size}
          {...restProps}
        />
      </div>
    )
  }
}

CustomTable.propTypes = {
  addNew: PropTypes.string,
  addNewText: PropTypes.string,
  loading: PropTypes.bool.isRequired,
  dataSource: PropTypes.array.isRequired,
  columns: PropTypes.array.isRequired,
  restProps: PropTypes.object,
  multiple: PropTypes.bool,
  scroll: PropTypes.object,
  onChange: PropTypes.func,
  pagination: PropTypes.object,
  size: PropTypes.string,
  selectedRowKeys: PropTypes.object,
  bordered: PropTypes.bool,
  multipleSelect: PropTypes.func
}
